package com.oocl.server.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class HttpRequest {
	private Socket socket;
	private String method;
	private String url;
	private String path;
	private Map<String, String> paraMap;

	public HttpRequest(Socket socket) {
		super();
		this.socket = socket;
		parserRequest();
		parserParameter();
	}

	public void parserRequest() {
		try {
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String[] header = bufferedReader.readLine().split(" ");
			this.method = header[0];
			this.url = header[1].substring(1, header[1].length());

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void parserParameter() {
		String[] parameters = null;
		String[] content = url.split("\\?");
		path = content[0];
		if (content.length == 2) {
			parameters = content[1].split("&");
			paraMap = new HashMap<String, String>();
			for (int i = 0; i < parameters.length; i++) {
				String[] currentLine = parameters[i].split("=");
				paraMap.put(currentLine[0], (String)currentLine[1]);
			}
		}
	}

	public String getParameter(String key) {
		return paraMap.get(key);
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

}
